"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**文件系统操作 */
const Fs = tslib_1.__importStar(require("fs"));
/**路径系统操作类 */
const Path = tslib_1.__importStar(require("path"));
/**基础数据处理 */
const m_base_1 = tslib_1.__importDefault(require("./m-base"));
/**读取目录 */
const readDir = (path) => {
    return new Promise((resolve, reject) => {
        Fs.readdir(path, (err, files) => {
            if (err)
                return reject(err);
            resolve(files);
        });
    });
};
/**创建目录 */
const createPath = (path) => {
    return new Promise((resolve, reject) => {
        if (Fs.existsSync(path))
            resolve(void 0);
        Fs.mkdir(path, { recursive: true }, (err) => {
            if (err)
                return reject(err);
            resolve(void 0);
        });
    });
};
/**读取文件内容 */
const readFile = (file) => {
    return new Promise((resolve, reject) => {
        Fs.readFile(file, (err, data) => {
            if (err)
                return reject(err);
            resolve(data.toString());
        });
    });
};
/**写入文件内容 */
const writeFile = (path, data) => {
    return new Promise((resolve, reject) => {
        Fs.writeFile(path, data, (err) => {
            if (err)
                return reject(err);
            resolve(void 0);
        });
    });
};
/**添加文件内容 */
const appendFile = (path, data) => {
    return new Promise((resolve, reject) => {
        Fs.appendFile(path, data, (err) => {
            if (err)
                return reject(err);
            resolve(void 0);
        });
    });
};
/**删除文件 */
const deleteFile = (path) => {
    return new Promise((resolve, reject) => {
        if (!Fs.existsSync(path))
            resolve(void 0);
        Fs.unlink(path, (err) => {
            if (err)
                return reject(err);
            resolve(void 0);
        });
    });
};
/**文件路径处理 */
class MFile {
    /**获取命令项目根目录 */
    static getRoot() {
        return MFile.getPath(MFile.getPath(__dirname));
    }
    /**获取文件或目录名称 */
    static getName(path, ext = true) {
        const name = Path.basename(path);
        if (ext)
            return name;
        return name.split(".")[0];
    }
    /**获取文件或目录的上级目录路径 */
    static getPath(path) {
        return Path.dirname(path);
    }
    /**组装网络路径 */
    static joinUrlPath(...paths) {
        paths = paths.map((x) => m_base_1.default.trimStr(x, "/"));
        paths = paths.filter((x) => !!x);
        return paths.join("/");
    }
    /**组装文件或者目录路径 */
    static joinDirPath(...paths) {
        paths = paths.filter((x) => !!x);
        paths = paths.map((x) => x.replace(/\//, Path.sep));
        return Path.join(...paths);
    }
    /**地址是否存在 */
    static isExist(path) {
        return Fs.existsSync(path);
    }
    /**地址是否一个文件 */
    static isFile(path) {
        if (!MFile.isExist(path))
            return false;
        return Fs.statSync(path).isFile();
    }
    /**是否一个文件夹 */
    static isDir(path) {
        if (!MFile.isExist(path))
            return false;
        return Fs.statSync(path).isDirectory();
    }
    /**文件内容读取 */
    static async readFile(file) {
        return await readFile(file);
    }
    /**文件写内容 */
    static async writeFile(path, data) {
        if (!MFile.isExist(Path.dirname(path))) {
            await MFile.createPath(Path.dirname(path));
        }
        return await writeFile(path, data);
    }
    /**文件添加内容[没文件会异常] */
    static async appendFile(path, data) {
        if (!MFile.isExist(Path.dirname(path))) {
            await MFile.createPath(Path.dirname(path));
        }
        return await appendFile(path, data);
    }
    /**文件删除 */
    static async deleteFile(path) {
        return await deleteFile(path);
    }
    /**创建目录 */
    static async createPath(path) {
        return await createPath(path);
    }
    /**读取目录 */
    static async readDir(path) {
        return await readDir(path);
    }
    /**删除目录 */
    static async deletePath(path) {
        if (!MFile.isExist(path))
            return;
        if (MFile.isFile(path))
            return await MFile.deleteFile(path);
        const childs = await MFile.readDir(path); //读取目录
        for (let i = 0; i < childs.length; i++) {
            const curPath = Path.join(path, childs[i]); //当前地址
            if (MFile.isDir(curPath)) {
                await MFile.deletePath(curPath); //递归删除目录
            }
            else if (MFile.isFile(curPath)) {
                await MFile.deleteFile(curPath); //删除文件
            }
        }
        Fs.rmdirSync(path); //删除空目录
    }
    /**检查路径，没有则创建[有则清空] */
    static async checkPath(path, del = false) {
        if (del && MFile.isDir(path))
            return await MFile.deletePath(path);
        if (!MFile.isDir(path))
            await MFile.createPath(path);
    }
}
exports.default = MFile;
